﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using BizProxy.Models;
using Creative85.Framework.Utilities;
using Project66KM.Biz.Unionshop;
using Project66KM.Biz.UserAccount;

namespace BizProxy
{
    /// <summary>
    /// 这个类即将废弃。drop
    /// </summary>
    public static class LicenseProxyService
    {
        public readonly static string BaseUrl = ConfigurationManager.AppSettings["ServiceBaseUrl"] + "License";
        public readonly static string Product = "66km-d1";

        private class GetResultModel
        {
            public bool isalert { get; set; }
            public DateTime date { get; set; }
        }

        public static void RegisterInit(Guid shopId)
        {
            var shop = ShopService.Get(shopId);
            {
                var ret = string.Empty;
                ret = RequestUtility.HttpPost(BaseUrl + "/RegisterInit", new
                {
                    unionId = shop.UnionID,
                    shopId = shop.ID,
                    product = Product
                }, Timeout.Infinite);
                var json = JsonUtility.FromJson<JsonResultModel<GetResultModel>>(ret);
                if (json.code != 0) { throw new NotSupportedException(json.msg); }
            }
        }
        public static DateTime Get(Guid shopId)
        {
            var shop = ShopService.Get(shopId);
            var result = DateTime.MinValue;
            {
                var ret = string.Empty;
                ret = RequestUtility.HttpPost(BaseUrl + "/Get", new
                {
                    unionId = shop.UnionID,
                    shopId = shop.ID,
                    product = Product
                }, Timeout.Infinite);
                var json = JsonUtility.FromJson<JsonResultModel<GetResultModel>>(ret);
                if (json.code != 0) { throw new NotSupportedException(json.msg); }
                result = json.data.date;
            }
            return result;
        }
        public static bool CheckExpiredTime(Guid shopId)
        {
            var expiredTime = Get(shopId);
            return (expiredTime <= DateTime.Now);
        }
        public static bool IsAlert(Guid shopId)
        {
            var shop = ShopService.Get(shopId);
            var result = false;
            {
                var ret = string.Empty;
                ret = RequestUtility.HttpPost(BaseUrl + "/Get", new
                {
                    unionId = shop.UnionID,
                    shopId = shop.ID,
                    product = Product
                }, Timeout.Infinite);
                var json = JsonUtility.FromJson<JsonResultModel<GetResultModel>>(ret);
                if (json.code != 0) { throw new NotSupportedException(json.msg); }
                result = json.data.isalert;
            }
            return result;
        }
        public static string GetBuyUrl(Guid shopId, Guid userId)
        {
            var shop = ShopService.Get(shopId);
            return string.Format("{0}/Payment?unionId={1}&shopId={2}&userId={3}&product={4}", BaseUrl, shop.UnionID, shop.ID, userId, Product);
        }
    }
}
